<template>
	<view>
		<u-sticky>
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs>
		</u-sticky>
		<view v-if="current == 0">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20">
				<u-search placeholder="请输入关键词搜索" :clearabled="true" v-model="keyword" border-color="#606266" @custom="search" @search="search"></u-search>
			</view>
			<u-card padding="16" v-for="(item,index) in kongxian" :key="index">
				<view class="" slot="head">
					<view class="wrap">
						<u-row>
							<u-col span="12" text-align="left">
								<view class="weikaishi">
									<span>签到时间：{{item.times}}</span>
								</view>
							</u-col>
						</u-row>
						<u-row>
							<u-col span="12" text-align="left">
								<view class="weikaishi">
									<span>装车时间：{{item.londingTime}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="" slot="body">
					<view class="wrap">
						<view>
							<u-row>
								<u-col span="12" text-align="left">
									<view class=" u-m-t-5">
										<span>签到人：{{item.createUsername}}</span>
									</view>
								</u-col>
								<u-col span="12" text-align="left">
									<view class=" u-m-t-5">
										<span>车牌号：{{item.v1}}</span>
									</view>
								</u-col>
								<u-col span="12" text-align="left">
									<view class=" u-m-t-5">
										<span>手机号：{{item.v2}}</span>
									</view>
								</u-col>
								<u-col span="12" text-align="center">
									<view class=" u-m-t-5 huanhang lanse">
										<span>定位：{{item.content}}</span>
									</view>
								</u-col>
								<u-col span="12" text-align="center">
									<u-button type="success"  @click="zhuangche(item)" v-if="item.londingTime == ''">开始装车</u-button>
								</u-col>
							</u-row>
						</view>
					</view>
				</view>
			</u-card>
			<view style="height: 90rpx;">
			</view>
			<view class="wrap" style="position: fixed;bottom: 0rpx;width: 100%;background-color: #2979ff;z-index: 999;">
				<u-line color="#bababa"/>
				<u-row>
					<u-col span="12" text-align="center" @click="qds()">
						<view class="u-line-1 u-m-t-10 baise">
							到仓签到
						</view>
					</u-col>
				</u-row>
			</view>
		</view>
		<view v-if="current == 1">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20">
				<u-search placeholder="请输入门店名称" :clearabled="true" v-model="keyword1" border-color="#606266" @custom="search1" @search="search1"></u-search>
			</view>
			<u-table>
				<u-tr class="u-tr">
					<u-th class="u-th" width="20%">门店</u-th>
					<u-th class="u-th" width="20%">巷道</u-th>
					<u-th class="u-th" width="10%">总托</u-th>
					<u-th class="u-th" width="10%">待拣</u-th>
					<u-th class="u-th" width="20%">待质</u-th>
					<u-th class="u-th" width="20%">甩货</u-th>
					
				</u-tr>
				<u-tr class="u-tr" v-for="(item,index) in dataList" :key="index">
					<u-td class="u-td" width="20%">{{item.getUnit}}</u-td>
					<u-td class="u-td" width="10%">{{item.stockPort}}</u-td>
					<u-td class="u-td" width="10%">{{item.ztCnt}}</u-td>
					<u-td class="u-td" width="10%">{{item.jhzCnt}}</u-td>
					<u-td class="u-td" width="20%">{{item.zjzCnt}}</u-td>
					<u-td class="u-td" width="20%">{{item.shCnt}}</u-td>
				</u-tr>
			</u-table>
		</view>
		<u-popup v-model="show" mode="center" closeable width="600rpx" height="620rpx" border-radius="10">
			<view class="u-text-center u-m-t-20 weikaishi2">选择项目</view>
			<view class=" u-m-20">
				<u-input v-model="operateName" type="select" @click="getO()" placeholder="请选择项目"/>
			</view>
			<view class="u-text-center u-m-t-20 weikaishi2">选择所属承运商</view>
			<view class=" u-m-20">
				<u-row>
					<u-col span="12" text-align="center">
						<view class="weikaishi2 u-m-t-15">
							<u-radio-group v-model="carrierName">
								<u-radio 
									@change="radioChange1" 
									v-for="(item, index) in carrierlist" :key="index" 
									:name="item.name"
								>
								{{item.name}}
								</u-radio>
							</u-radio-group>
						</view>
					</u-col>
				</u-row>
			</view>
			<view  style="height: 80rpx">
				<view class="close-btn u-m-t-40">
					<u-button size="medium" @click="qd()">确定</u-button>
				</view>
			</view>
		</u-popup>
		<view>
			<u-select v-model="show2" :list="olist" @confirm="confirm"></u-select>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	import config from '../../../config.js'
	export default {
		data() {
			return {
				flex:'',
				keyword:"",
				keyword1:"",
				current: 0,
				list: [{
						name: '到仓签到'
					},
					{
						name: '货品查询'
					}
				],
			    pageNo: 1,
			    hasMore: true,
			    orderCount: 0,
				kongxian:[],
				dataList:[],
				operateId:'',
				operateName:'',
				
				show:false,
				carrierName:'',
				carrierlist: [],
				olist: [],
				show2:false,
			}
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			
			//注意:将代码放在onLoad方法下
			//#ifdef MP-WEIXIN
			if (!wx.getStorageSync('hadAuthed')) {
				wx.login({
				  success (res) {
				    if (res.code) {
					  uni.request({
					  	method:"POST",
					  	header:{
					  		'Authorization' :uni.getStorageSync('token'),
					  	},
					  	url: config.root +'/rest/transport/getopenId?code='+res.code,
					  	data:{
					  	},
					  	success: (res) => {
					  		if(res.data.code == 200) {
								if(res.data.data != null && res.data.data != ''){
									wx.setStorageSync('openId', res.data.data)
									wx.setStorageSync('hadAuthed', true)
								}
					  		} 
					  	}
					  })
				    } else {
				      console.log('登录失败！' + res.errMsg)
				    }
				  }
				})
			}
			//#endif
			
			this.$http.post('/rest/operate/findme',{
			},{
				params: {
				},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				if(res.data.data != '' && res.data.data != null){
					this.operateId = res.data.data.id
					this.operateName = res.data.data.operateName
					this.$http.post('/rest/driverAndCar/findCarrierList',{
					},{
						params: {
							operateId:res.data.data.id
						},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						for(let item1 of res.data.data) {
							let obj = {}
							obj.name = item1.carrierName
							this.carrierlist.push(obj)
						}
					})
				}
			})
			
			this.$http.post('/rest/operate/list3',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				for(let item1 of res.data.data) {
					let obj = {}
					obj.value = item1.id
					obj.label = item1.operateName
					this.olist.push(obj)
				}
			})
			
			this.$http.post('/rest/driverAndCar/findCar',{
			},{
				params: {
				},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				if(res.data.data != '' && res.data.data != null){
					if(res.data.data.privatecarFleetName != null && res.data.data.privatecarFleetName != ''){
						this.carrierName = res.data.data.privatecarFleetName
					}
				}
			})
			
			uni.showLoading({
				title: '加载中'
			})
			this.refresh()
			setTimeout(() => {
				uni.hideLoading()
			}, 500)
		},
		onPullDownRefresh() {
			uni.showLoading({
				title: '加载中'
			})
			if(this.current == 0){
				this.refresh()
			}else{
				this.getDataList();
			}
			setTimeout(() => {
				uni.hideLoading()
				uni.stopPullDownRefresh()
			}, 500)
		},
		onReachBottom() {
			uni.showLoading({
				title: '加载中'
			})
			if(this.current == 0){
				if (this.hasMore) {
					this.pageNo++ 
					setTimeout(() => {
						this.loadOrder({
							keyword:this.keyword,
						}).then(res => {})
						uni.hideLoading()
					}, 500)
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}else{
				this.getDataList();
			}
		},
		methods: {
			zhuangche(item){
				let that = this;
				uni.showModal({
					content: '确定要开始装车吗？',
					success: (res) => {
						if (res.confirm) {
							uni.request({
								method:"POST",
								header:{
									'Authorization' :uni.getStorageSync('token'),
								},
								url: config.root +'/rest/transport/loadingTime?id='+item.id,
								data:{
								},
								success: (res) => {
									if(res.data.code == 200) {
										uni.showToast({
											title: '操作成功',
											duration: 2000
										});
										setTimeout(() => {
											that.refresh()
										}, 500)
									} else {
										uni.showToast({
											title: res.data.message,
											duration: 2000,
											icon:'none',
										});
									}
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
				
			},
			sectionChange(e) {
				this.current = e
				if(this.current == 1){
					this.$http.post('/rest/transport/getQx1', { //校验司机 个人信息完善
					}, {
						params: {},
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						if (res.data.code != 200) {
							uni.showToast({
								title: res.data.message,
								duration: 2000,
								icon: 'none'
							});
							this.current = 0;
						}
					})
				}
			},
			carrier(operateId){
				this.$http.post('/rest/driverAndCar/findCarrierList',{
				},{
					params: {
						operateId:operateId
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let newarr = []
					for(let item1 of res.data.data) {
						let obj = {}
						obj.name = item1.carrierName
						newarr.push(obj)
					}
					this.carrierlist = newarr
				})
			},
			confirm(e) {
				this.operateId = e[0].value
				this.operateName = e[0].label
				this.carrier(e[0].value)
			},
			getO(){
				this.show2 = true
			},
			radioChange1(e){
				console.log(e)
			},
			qds(){
				this.show = true
			},
			qd(){
				if(this.operateId == '' || this.operateId == null){
					uni.showToast({
						title: "请选择项目",
						icon: 'none'
					})
					return
				}
				if(this.carrierName == '' || this.carrierName == null){
					uni.showToast({
						title: "请选择承运商",
						icon: 'none'
					})
					return
				}
				uni.showModal({
					content: '确定到仓签到吗？',
					success: (res) => {
						if (res.confirm) {
							var that = this
							var openId = wx.getStorageSync('openId')
							uni.getLocation({
								type:'gcj02',
								altitude:false,
								success: function (res) {
									const latitude = res.latitude;
									const longitude = res.longitude;
									uni.request({
										method:"POST",
										header:{
											'Authorization' :uni.getStorageSync('token'),
										},
										url: config.root +'/rest/transport/driverSignin?longitude='+longitude+'&latitude='+latitude+'&operateId='+that.operateId+'&carrierName='+that.carrierName+'&+openId='+openId,
										data:{
										},
										success: (res) => {
											that.show = false
											if(res.data.code == 200) {
												uni.showToast({
													title: '操作成功',
													duration: 2000
												});
												setTimeout(() => {
													that.refresh()
												}, 30000)
												clearInterval(uni.getStorageSync('intervalId'));
												uni.removeStorageSync('carnumber')
											} else {
												uni.showToast({
													title: res.data.message,
													duration: 2000,
													icon:'none',
												});
											}
										}
									})
								},
								fail:function(res){
									uni.showToast({
										title: '获取当前定位失败',
										duration: 2000,
										icon:'none',
									});
								},
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			refresh(){
				this.pageNo = 1
				this.hasMore = true
				this.loadOrder({
					keyword:this.keyword,
				}).then(res => {
				})
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 10
					})
					this.$http.post('/rest/transport/finddriverSigninListPageBySpec',{
					},{
						params: param,
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						if (this.pageNo == 1) {
							this.kongxian = res.data.data
						} else {
							this.kongxian = this.kongxian.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.hasMore = this.pageNo * 10 < res.data.count
						resolve(res)
					})
				})
			},
			search(e){
				uni.showLoading({
					title: '加载中'
				})
				this.refresh()
				setTimeout(() => {
					uni.hideLoading()
				}, 500)
			},
			getDataList(){
				if(this.keyword1 == '' || this.keyword1 == null){
					uni.showToast({
						title: "请输入门店名称",
						icon: 'none'
					})
					return
				}
				this.$http.post('/rest/transport/goodQueryList',{
				},{
					params: {
						getUnit:this.keyword1
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.dataList = res.data.data
				})
			},
			search1(e){
				this.getDataList();
			}
		}
	}
</script>
<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}
	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}
	.weikaishi2{
		font-size: 34rpx;
	}
	.baise{
		color: #ffffff;
		font-size: 34rpx;
	}
	.weikaishi3{
		font-size: 30rpx;
	}
	.baise{
		background-color: #2979ff;
		color: #ffffff;
		font-size: 34rpx;
	}
	.huanhang{
		word-break:break-all;
	}
	.margin-zdy{
		padding: 5rpx 20rpx 5rpx 30rpx;
		background-color: #FFFFFF;
	}
</style>
